#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

int* array;
int n;

void append(int *p,int num)
{ int i;
  p=realloc(p , (n+1)*sizeof(int));
  p[n]=num;
  n=n+1;
  
 }
       
void display(int *p)
{   int i;
    printf("\nThe List is: \n");
    printf("[");    
    for (i=0; i<n; i++) 
{
  printf(" %d ",p[i]);
}
printf("]");
}
int insert(int i,int x)
{ int j;
  n=n+1;
 array=realloc(array , (n+1)*sizeof(int));
 for(j=n;j>=i;j--)
   { array[j+1]=array[j];
} 
   array[i]=x;
   return n;
}

int delete(int x)
{ int i,j,flag=0;
  for(i=0;i<n;i++)
   {  if(array[i]==x)
      { 
	flag=1;
	for(j=i;j<n;j++)
				{
					array[j]=array[j+1];
				}
			break;	
			}
		}
	if (flag ==0)
	printf ("element not found ");
	n--;
	return n;	
}
void reverse() 
{ int i;  
  printf("\n THE REVERSE OF THE LIST IS:  [");
  for(i=(n-1);i>=0;i--)
{ printf(" %d ",array[i]);
}
printf("]");

}

int pop (int i)
{
	int j,k;
	k=array[i-1];
	for(j=i+1;j<=n;j++)
	{
		array[j-1]=array[j];
		
	}
	n--;
	return k;
	return n;
}
int count_n(int x)
{ int i,ctr=0;
  for(i=0;i<n;i++)
  {  if(array[i]==x)
     { ctr++;
}
}
return ctr;
}
void index1(int x)
{ int i;
  for(i=0;i<n;i++)
  {  if(array[i]==x)
     
     break;
}
printf("%d OCCURS AT LOCATION %d IN THE LIST",x,i);
}
void b_sort()
{   int i,j,temp;
   for(i=0;i<n;i++)
   {  for(j=i;j<n;j++)
     {  if(array[i]> array[j])
        { temp=array[i];
          array[i]=array[j];
          array[j]=temp;
}
}
}
}


main() {
  
  int i,num,x,j,k, a[100];
  array = (int*) malloc(n*sizeof(int));
 

do
 {
    
    
    printf("\n 1.APPEND AN ELEMENT INTO THE LIST:");
    printf("\n 2.INSERT A NUMBER AT A PARTICULAR LOCATION IN THE LIST:");
    printf("\n 3.PRINT THE LIST :");
    printf("\n 4.PRINT THE NUMBER OF ELEMENTS IN THE LIST  ");
    printf("\n 5.REMOVE AN ELEMENT FROM THE LIST:");
    printf("\n 6.PRINT REVERSE OF THE LIST :");
    printf("\n 7.POP THE ELEMENT AT PARTICULAR LOCATION:");
    printf("\n 8.COUNT NO. OF OCCURENCES OF AN ELEMENT:");
    printf("\n 9.EXTEND THE LIST :");
    printf("\n 10.FIND INDEX OF THE FIRST OCCURENCE OF AN ELEMENT :"); 
    printf("\n 11.SORT THE LIST :");
    printf("\n 12.EXIT:");
    printf("\n PLEASE, ENTER YOUR CHOICE:");
    scanf("%d",&i);

      switch(i)
    {      
          case 1: 
                   printf("\n PLEASE ENTER THE NUMBER :-");
                   scanf("%d",&num);
                   append(array,num);
                   break;
       

          case 2: 
                  printf("PLEASE ENTER THE NUMBER :-");
      		  scanf("%d",&x);
      		  printf("PLEASE ENTER THE LOCATION NUMBER :-");
      	          scanf("%d",&j);
                  insert(j,x);
                  printf("NOW THE LIST IS:");
                  display(array);
                  break;
          case 3: printf("\n THE  ELEMENTS IN THE LIST ARE : ");
         	  display(array);
         	  break;
      

          case 4: 
                  display(array);
                  printf(" TOTAL NO OF ELEMENTS IN THE LIST ARE %d",n);
      		  break;
          case 5: 
                  printf("PLEASE ENTER A NUMBER FROM THE LIST :");
      		  scanf("%d",&num);
      		  delete(num);
     		  break;
    
   	  case 6: reverse(array);
        	  break;
 
          case 7: 
                  printf("\n ENTER THE LOCATION WHOSE ELEMENT IS TO BE POPPED:");
                  scanf("%d",&j);
                  pop(j);
                  break; 
         case 8:  printf("PLEASE ENTER A NUMBER FROM THE LIST :");
                  scanf("%d",&num);
                  printf("NO. OF TIMES %d OCCURS IN THE LIST=%d",num,count_n(num));
                  break; 
         case 9:  
                  printf("ENTER no. of ELEMENTS IN THE NEW LIST:");
                  scanf("%d",&k);
		  printf("ENTER ELEMENTS IN THE NEW LIST:");
                  for(i=0;i<k;i++)
		  {        scanf("%d",&j);
                           append(array,j);
                           
                        }
                  printf("\n THE EXTENDED LIST IS:");
                  display(array); 
		  break;
        case 10:  printf("ENTER ELEMENT WHOSE INDEX IS REQUIRED:");
		  scanf("%d",&j);
                  index1(j);
		  break;  
        case 11:  printf("\n THE SORTED LIST IS:");
                  b_sort();
                  display(array);
                  break;
        case 12: exit(0);
    }
 }while((i>=1)&&(i<=11));
}
 
  

